﻿using System;
using System.Data;
using System.Text;
using System.IO;

namespace ProductionTools
{
    class Program
    {
        private static string txtFilesName = @"../Model/ect/";
        static void Main(string[] args)
        {
            // DataTable dt = DB.MySqlDb().ExecuteDataTable("select * from Permit_Menu", null);
            // int ss = dt.Rows.Count;
            // for (int i = 0; i < dt.Rows.Count; i++)
            // {
            //     string d = dt.Rows[i][0].ToString();
            // }
            GetTable();
        }
        public static void GetTable()
        {
            DataTable dt = null;


            dt = DB.MySqlDb().ExecuteDataTable("SHOW TABLES;", null);


            if (dt != null && dt.Rows.Count == 0)
                return;


            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Generate(dt.Rows[i][0].ToString());
            }
        }

         public static void Generate(string table)
        {

            DataTable dt = null;
            string sql = "";
           
                sql = @"select column_name 字段名称, data_type 字段类型, column_comment 注释, column_key 是否主键, extra from                   information_schema.columns
                         where table_name = '" + table + "' and table_schema = (select database()) order by ordinal_position";


           
            dt = DB.MySqlDb().ExecuteDataTable(sql, null);
            if (dt == null || dt.Rows.Count == 0)
                return;


            StringBuilder sb = new StringBuilder();
            sb.Append("using System;\n");
            sb.Append("using System.Collections.Generic;\n");
            sb.Append("using System.Linq;\n");
            sb.Append("using System.Text;\n");
            sb.Append("using System.Threading.Tasks;\n\n\n");
            sb.Append("namespace Model\n");
            sb.Append("{\n");
            sb.Append("    public  partial class " + table.Substring(0, 1).ToUpper() + table.Substring(1));
            sb.Append("\n   {\n");

            foreach (DataRow myRow in dt.Rows)
            {
                sb.Append("      /// <summary>\n");
                sb.Append("      ///" + myRow["注释"].ToString());
                sb.Append("\n      /// <summary>\n");
                sb.Append("      public " + getFieldType(myRow["字段类型"].ToString()) + " " + myRow["字段名称"].ToString() + " { get; set; }\n");
                // var str = myRow[0].ToString();
            }
            sb.Append("    }\n");
            sb.Append("}\n");

            string url = @"" + txtFilesName + table.Substring(0, 1).ToUpper() + table.Substring(1) + "BM.cs";

            if (File.Exists(url))
            {
                File.Delete(url);
            }

            FileStream fs1 = new FileStream(url, FileMode.Create, FileAccess.Write);//创建写入文件
            StreamWriter sw = new StreamWriter(fs1);
            sw.WriteLine(sb);//开始写入值
            sw.Close();
            fs1.Close();

        }


         public static String getFieldType(String columnType)
        {
            /*
             * tinyblob tinyblob byte[] 
                tinytext varchar java.lang.string 
                blob blob byte[] 
                text varchar java.lang.string 
                mediumblob mediumblob byte[] 
                mediumtext varchar java.lang.string 
                longblob longblob byte[] 
                longtext varchar java.lang.string 
                enum('value1','value2',...) char java.lang.string 
                set('value1','value2',...) char java.lang.string 
             */
            if (columnType == "varchar" || columnType == "nvarchar"
                     || columnType == "char" || columnType == "text"
                    // || columnType == "tinytext")
                    // || columnType == "text") 
                    // || columnType == "mediumtext")
                    // || columnType == "longtext")
                    )
            {
                return "String";
            }
            else if (columnType == "tinyblob"
                  || columnType == "blob"
                  || columnType == "mediumblob"
                  || columnType == "longblob")
            {
                return "byte[]";
            }
            else if (columnType == "datetime"
                  || columnType == "date"
                  || columnType == "timestamp"
                  || columnType == "time"
                  || columnType == "year")
            {
                return "DateTime";
            }
            else if (columnType == "bit"
                  || columnType == "int"
                  || columnType == "tinyint"
                  || columnType == "smallint"
                  // ||columnType == "bool")        
                  // ||columnType == "mediumint")        
                  // ||columnType == "bigint")
                  )
            {
                return "int";
            }
            else if (columnType == "float")
            {
                return "Float";
            }
            else if (columnType == "double")
            {
                return "Double";
            }
            else if (columnType == "decimal")
            {
                return "Decimal";
                // sbpackage.append("import java.math.BigDecimal;\r\n");
                // return "BigDecimal";
            }else if(columnType == "bigint")
            {
                return "long";
            }
            return "string";
        }


    }
}
